/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import org.synyx.hades.domain.AbstractPersistable;

import javax.persistence.*;
import java.util.List;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "ConfigLocation.findByAll", query = "from ConfigLocation o"),
		@NamedQuery(name = "ConfigLocation.findByEmptyConfigLocationGroup", query ="from ConfigLocation o where o.configLocationGroup is null"),
		@NamedQuery(name = "ConfigLocation.findByVersion", query = "from ConfigLocation o where o.version.major <= ? and o.version.minor <= ?")})		
public class ConfigLocation extends AbstractPersistable<Long> {
	public enum ConfigLocationType {
		DIRECTORY, PROPERTY_FILE
	}

	@Column(unique = true)
	private String name;
	private String location;
	
	@Enumerated(EnumType.STRING)
	private ConfigLocationType type;
	
	@ManyToOne
	private ConfigLocationGroup configLocationGroup;
	
	@ManyToOne
	private Version version;
	
	@OneToMany(mappedBy="configLocation")
	private List<ConfigPair> configPairs;

	@OneToMany(mappedBy="configLocation")
	private List<Attachment> attachments;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public ConfigLocationType getType() {
		return type;
	}

	public void setType(ConfigLocationType type) {
		this.type = type;
	}

	public void setConfigLocationGroup(ConfigLocationGroup configLocationGroup) {
		this.configLocationGroup = configLocationGroup;
	}

	public ConfigLocationGroup getConfigLocationGroup() {
		return configLocationGroup;
	}

	public void setVersion(Version version) {
		this.version = version;
	}

	public Version getVersion() {
		return version;
	}

	public void setConfigPairs(List<ConfigPair> configPairs) {
		this.configPairs = configPairs;
	}

	public List<ConfigPair> getConfigPairs() {
		return configPairs;
	}

	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}

	public List<Attachment> getAttachments() {
		return attachments;
	}

	@Override
	public String toString() {
		return "ConfigLocation [configLocationGroup=" + configLocationGroup
				+ ", location=" + location + ", name=" + name + ", type="
				+ type + ", version=" + version + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime
				* result
				+ ((attachments == null) ? 0 : attachments.hashCode());
		result = prime
				* result
				+ ((configLocationGroup == null) ? 0 : configLocationGroup
						.hashCode());
		result = prime * result
				+ ((configPairs == null) ? 0 : configPairs.hashCode());
		result = prime * result
				+ ((location == null) ? 0 : location.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + ((version == null) ? 0 : version.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof ConfigLocation)) {
			return false;
		}
		ConfigLocation other = (ConfigLocation) obj;
		if (attachments == null) {
			if (other.attachments != null) {
				return false;
			}
		} else if (!attachments.equals(other.attachments)) {
			return false;
		}
		if (configLocationGroup == null) {
			if (other.configLocationGroup != null) {
				return false;
			}
		} else if (!configLocationGroup.equals(other.configLocationGroup)) {
			return false;
		}
		if (configPairs == null) {
			if (other.configPairs != null) {
				return false;
			}
		} else if (!configPairs.equals(other.configPairs)) {
			return false;
		}
		if (location == null) {
			if (other.location != null) {
				return false;
			}
		} else if (!location.equals(other.location)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (type == null) {
			if (other.type != null) {
				return false;
			}
		} else if (!type.equals(other.type)) {
			return false;
		}
		if (version == null) {
			if (other.version != null) {
				return false;
			}
		} else if (!version.equals(other.version)) {
			return false;
		}
		return true;
	}
}
